#include"shmcomm.hpp"
int main()
{
    //获取key值
    key_t k=ftok(PATH_NAME,PROJ_ID);
    assert(k!=-1);
    printf("key:%d\n",k);

    //获取共享内存
    int shmid=shmget(k,SHM_SIZE,IPC_CREAT);
    if(shmid==-1)
    {
        perror("shmget");
        exit(1);
    }

    //挂接
    char*shmaddr=(char*)shmat(shmid,nullptr,0);
    if(shmaddr==nullptr)
    {
        perror("shmat");
        exit(2);
    }

    //进程间通信
    int count=0;
    while(count<10)
    {
        snprintf(shmaddr,SHM_SIZE-1,"hello server,我是另一个进程,正在和你通信,我的pid:%d  count:%d",getpid(),count++);
        sleep(1);
    }
    strcpy(shmaddr,"quit");

    //去关联
    int m=shmdt(shmaddr);
    assert(m!=-1);
    (void)m;

    return 0;
}